﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WPFExcelReport.Controls;
using WPFExcelReport.Helpers;
using System;
using System.Threading.Tasks;
namespace WPFExcelReport
{
    public partial class UIAM
    {
        private DataTable _JJRules;
        public DataTable JJRules
        {
            get
            {
                if (_JJRules == null)
                {
                    _JJRules = TMV.JJRULES.TBMain.Copy();
                }
                return _JJRules;
            }
            set
            {
                _JJRules = value;
            }
        }
       

        public string MachineID
        {
            get;
            set;
        }
        public string MKEY
        {
            get;
            set;
        }

        public DataRow dwMJob
        {
            get;
            set;
        }


        private bool _IsShow = false;
        public bool IsShow
        {
            get { return _IsShow; }
            set
            {
                _IsShow = value;

                if (_IsShow)
                {
                    this.Visibility = Visibility.Visible;
                    MGANTT.Visibility = Visibility.Visible;
                }
                else
                {
                    this.Visibility = Visibility.Collapsed;
                    MGANTT.Visibility = Visibility.Collapsed;
                }
                this.TVHeight = MGANTT.TVHeight;
            }
        }
        public MCALENDAR MCALENDAR
        {
            get;
            set;
        }
        public MGANTT MGANTT
        {
            get;
            set;
        }
        public DataTable JOBS
        {
            get;
            set;
        }
        public DataRow dwMachine
        {
            get;
            set;
        }

        System.Windows.Controls.Border Border
        {
            get
            {
                return (System.Windows.Controls.Border)this.FindName("border");
            }
        }
        
        public virtual void SetUp(bool IsEven)
        {
            MJQTY = "0";
            MJNUM = "0";
            MKEY = dwMachine["AMNRKRS"].ToString() + dwMachine["AMLFDNR"].ToString() + dwMachine["AMPOS"].ToString();
            MachineID = dwMachine["MachineID"].ToString();
            DES = dwMachine["AMBEZ"].ToString().Trim();
            this.Name = MachineID;
            PrepareMGANTT(IsEven);
        }
        public bool IsEven = false;
        public void PrepareMGANTT(bool _IsEven)
        {
            IsEven = _IsEven;
            MGANTT = new MGANTT();
            MGANTT.dwMachine = dwMachine;
            MGANTT.HorizontalAlignment = HorizontalAlignment.Left;
            MGANTT.MCALENDAR = MCALENDAR;
            MGANTT.Machine = this;
            MGANTT.SetUp(IsEven);
        }
        
        public string SMTYP
        {
            get { return dwMachine["SMTYP"].ToString(); }
        }
        public Dictionary<string, object> JobsLib;

        public DateTime CalendarStart
        {
            get
            {
                return MCALENDAR.CalendarStart;
            }
            set
            {
                MCALENDAR.CalendarStart = value;
            }
        }

        public override void Dispose(bool disposing)
        {
            TMV.AMClient.UNRegister("UNREG", MKEY);
            base.Dispose(disposing);
        }
       
        public virtual void ReSet(DataSet db, string sdate, string stime)
        {
            LastEndDate = sdate;//AMAENDDAT
            LastEndTime = stime;//AMNEUDAT
            if (TBRILI == null)
            {
                AvailibleRili = 0;
            }
            if (TBWD == null)
            {
                
            }
            
            #region=====开始生产任务=====
            int idx = 1;
            var qjob = from DataRow dwJob in db.Tables["ICC_MKRF"].AsEnumerable()
                       where
                       dwJob["MACHINEID"].ToString() == MachineID
                       && dwJob["JOBTYPE"].ToString().Trim() == "P"
                       orderby dwJob.Field<Decimal>("ABFISTARTDAT") ascending
                       orderby dwJob.Field<Decimal>("ABFISTARTZT") ascending
                       select dwJob;
            foreach (DataRow dwJob in qjob)
            {
                dwJob["MKFLFDNR"] = idx;
                idx++;
                PushJob(dwJob);
                TMV.VMK.UpdateICC_MKRF(dwJob, 1);
            }
            #endregion
            #region=====已经确定的排产任务=====

            qjob = from DataRow dwJob in db.Tables["ICC_MKRF"].AsEnumerable()
                   where
                   dwJob["MACHINEID"].ToString() == MachineID
                   && dwJob["JOBTYPE"].ToString().Trim() == "C"
                   orderby dwJob.Field<Decimal>("MKFPLDATV") ascending
                   orderby dwJob.Field<Decimal>("MKFPLTIME") ascending
                   select dwJob;
            foreach (DataRow dwJob in qjob)
            {
                dwJob["MKFLFDNR"] = idx;
                idx++;
                PushJob(dwJob);
                TMV.VMK.UpdateICC_MKRF(dwJob, 1);
            }
            #endregion
            #region=====日历表任务=====
            //不计算开始结束时间
            qjob = from DataRow dwJob in TBRILI.AsEnumerable()
                   where
                   dwJob["MACHINEID"].ToString() == MachineID
                   && dwJob["JOBTYPE"].ToString().Trim() == "X"
                   orderby dwJob.Field<Decimal>("MKFPLDATV") ascending
                   orderby dwJob.Field<Decimal>("MKFPLTIME") ascending
                   select dwJob;
            foreach (DataRow dwJob in qjob)
            {
                dwJob["MKFLFDNR"] = idx; 
                dwJob["JP49"] = 0; 
                idx++;
                //PushJob(dwJob);日历任务应当一起参与排产，并更新设备最后的可用时间，表明设备的那个时刻的状态
            }
            #endregion
        }
       
         

        public void ShowJobs()
        {
            try
            {

                int idx = 0;
                foreach (DataRow dwJob in JOBS.Rows)
                {
                    UIJOB iJob = null;
                    string Jobid = "Job" + dwJob["ABAID"].ToString();
                    iJob = (UIJOB)JobsLib[Jobid];
                    if (iJob == null)
                        continue;
                    iJob.dwJob = dwJob;
                    iJob.PositionJob(this);
                    idx++;
                    if (idx > 100)
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


    }

}
